package com.itheima.app.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.UserInfoVo;
import com.itheima.service.db.UserService;
import com.itheima.util.JwtUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class UserController {

    //@Reference //使用dubbo的远程注入
    //private UserService userService;

    @Autowired //注入自己的代码管理类
    private UserManager userManager;
    //保存用户测试方法
    @PostMapping("/save")
    public Long save(@RequestBody User user){
        //1.判断手机号是否存在
        //2.判断手机号是否有效
        //3.判断手机号格式....
        return userManager.save(user);
    }

    //根据手机号进行查询
    @GetMapping("/findByPhone")
    public User findByPhone(String phone){
        //1.判断手机号是否存在
        //2.判断手机号是否有效
        //3.判断手机号格式....
        //数据的封装
        return userManager.findByPhone(phone);
    }


    /**
     * 传递的是json数据 需要使用对象来接收  此处使用的是map接收
     * @param map
     */
    //访问此方法 发送验证码
    @PostMapping("/user/login")
    public void sendSms(@RequestBody Map<String,String> map){
        String phone = map.get("phone");
        //调用manager 发送验证码
        userManager.sendSms(phone);
    }


    //登录或者注册
    @PostMapping("/user/loginVerification")
    public Map regOrLogin(@RequestBody Map<String,String> map){
        String phone = map.get("phone"); //手机号
        String verificationCode = map.get("verificationCode");//验证码
        //调用manager 发送验证码
        Map returnMap = userManager.regOrLogin(phone , verificationCode);
        return returnMap;
    }


    //完善个人信息
    @PostMapping("/user/loginReginfo")
    public Map loginReginfo(@RequestBody UserInfo userInfo , @RequestHeader("Authorization") String token){
        //校验token  解析token
        //User user = parseToken(token);
        User user = UserHolder.get();
        //赋值用户的id
        userInfo.setId( user.getId() );
        //保存用户对象
        return userManager.saveUserInfo(userInfo);
    }


    //完善个人信息-头像上传
    @PostMapping({"/user/loginReginfo/head" , "/users/header"} )
    public Map uploadHeader(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {
        //校验token  解析token
        //User user = parseToken(token);
        User user =UserHolder.get();

        //保存用户对象
        return userManager.updateUserInfo(headPhoto , user.getId() );
    }


    //根据用户id查询个人信息 ->  有坑
    @GetMapping("/users")
    public UserInfoVo findByUser(@RequestHeader("Authorization") String token , Long userID , Long huanxinID) throws IOException {

        //根据token查询用户对象 有可能根据userID查询对象  根据 环信ID查询对象
        //User user = parseToken(token);

        User user = UserHolder.get();
        UserInfoVo userInfo = null; //有坑
        /*if(user.getId()!=null){
            userInfo = userManager.findByUserId(user.getId());
        }else if(userID!= null){
            userInfo = userManager.findByUserId(userID);
        }else if(huanxinID != null){
            userInfo = userManager.findByUserId(huanxinID);
        }*/
        if( huanxinID != null){
            userInfo = userManager.findByUserId(huanxinID);
        }else if(user.getId()!=null){
            userInfo = userManager.findByUserId(user.getId());
        }else if(userID!= null){
            userInfo = userManager.findByUserId(userID);
        }
        System.out.println(userInfo);
        return userInfo;
    }


    //修改用户的个人信息
    @PutMapping("/users")
    public Map updateUserInfo(@RequestHeader("Authorization") String token ,@RequestBody UserInfo userInfo) throws IOException {

        userManager.updateUserInfo(userInfo);
        return null;
    }

    //解析token
    /*public User parseToken(String token){
        //解析token形成map
        Map map = JwtUtil.parseToken(token);
        //将map转换成对象
        User user = BeanUtil.mapToBean(map, User.class, true);
        return user;
    }*/

    @PostMapping("/users/phone/sendVerificationCode")
    public void sendSmss(){
        Long id = UserHolder.getId();

        userManager.sendSmss(id);
    }
    @PostMapping("/users/phone/checkVerificationCode")
    public Map checkVerificationCode(@RequestBody Map<String,String>map){
        String code = map.get("verificationCode");
        System.out.println(code+11);
        Map map1= userManager.checkVerificationCode(code);
        return map1;
    }
    @PostMapping("/users/phone")
    public Map updatePhone(@RequestBody Map<String,String>map){

        Long id = UserHolder.getId();
        String phone = map.get("phone");
        userManager.updatePhone(phone,id);
        return null;
    }

}
